#python packages
import os

# google appengine packages
from google.appengine.api import users
from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

# local packages
import datamodel
import process
import school
import course
import forms
from admin import Schools
from links import Links
from globals import Globals

class MainPage(webapp.RequestHandler):
    def get(self):
        
#        mich = datamodel.School(schName='University of Michigan',
#                        schAbbr='UMICH',
#                        webUrl='http://umich.edu',
#                        emailSfx='umich.edu')
#        mich.put()
#        datamodel.Department(school=mich,
#                     dName='Physics',
#                     dAbbr='PHYS').put()
#        datamodel.Department(school=mich,
#                     dName='Chemistry',
#                     dAbbr='CHEM').put()
        globals = Globals(True)
        
        user = globals.user

        if user:
            
            q = datamodel.School.all()
            q.order("schName")
            
            if q.count() > 0:
                schResults = q.fetch(10)
                
                schTable = '<table>'
                for sch in schResults:
                    schTable += ('<tr><td><a href="/schools?sch=%s">%s</a></td><td>%s</td></tr>' %
                                 (sch.key(), sch.schName, sch.webUrl))
                schTable += '</table>'
            else:
                schTable = '<p>No schools found</p>'
            
        else:            
            schTable = ''
            
        template_values = {
                           'globals': globals,
                           'schTable': schTable
                           }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/main.html')
        self.response.out.write(template.render(path, template_values))   

class About(webapp.RequestHandler):
    def get(self):
        template_values = {
                           'globals': Globals()
                           }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/about.html')
        self.response.out.write(template.render(path, template_values))
        
            
        
class AddDept(webapp.RequestHandler):
    def get(self):
        
        template_values = {
                           'globals': Globals(),
                           'school': self.request.get('sch')}
        
        path = os.path.join(os.path.dirname(__file__), 'templates/adddept.html')
        self.response.out.write(template.render(path, template_values))
        
class AddCourse(webapp.RequestHandler):
    def get(self):
        
        globals = Globals()
        
        deptList = self.getDeptsList()
        
        form = '<form method="POST" action="/process">\n'
        form += '<table>\n'
        form += '<tr><th><label for="id_dept">Dept: </label></th><td>' + deptList + '</td></tr>\n'
        form += str(forms.AddCourse())
        form += '<tr><td></td><td><input type="submit" value="Create" /></td></tr>\n'
        form += '</table>\n'
        form += '<input type="hidden" name="nsfn" value="addcourse" />\n'
        form += '</form>\n'
        
        memcache.add("form", form)
        
        template_values = {
                           'globals': globals,
                           'deptList': deptList,
                           'form': form}
        
        path = os.path.join(os.path.dirname(__file__), 'templates/addcourse.html')
        self.response.out.write(template.render(path, template_values))
        
    def getDeptsList(self):
        school = db.get(db.Key(self.request.get('sch')))
        depts = school.departments
        depts.order('dName')
        
        deptList = '<select name="dept" id="id_dept">\n'
        deptList += '<option value="">----</option>\n'
        for dept in depts:
            deptList += ('<option value="%s">%s</option>\n' % (dept.key(), dept.dName))
        deptList += ('</select> <a href="/adddept?sch=%s">Add Department</a>\n' % self.request.get('sch'))
        
        return deptList

application = webapp.WSGIApplication([('/', MainPage),
                                      ('/admin/schools', Schools),
                                      ('/process', process.Process),
                                      ('/about', About),
                                      ('/schools', school.School),
                                      ('/addcourse', AddCourse),
                                      ('/adddept', AddDept),
                                      ('/course', course.Course)],
                                      debug=True)

def main():
    run_wsgi_app(application)


if __name__ == "__main__":
    main()
